<?php

/**
 * photo actions.
 *
 * @package    maiphoto
 * @subpackage photo
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class photoActions extends sfActions {
    
    public function executeIndex(sfWebRequest $request) {
	$pageId = $request->getParameter('id');
	$countForPage = 6;
        $this->photos = Doctrine_Core::getTable('photo')->getInstance()->getLastPhotos($countForPage, $pageId*$countForPage);
        
        $this->numPhotos = PhotoTable::getInstance()->findAll()->count();
        $this->pagesCount = ceil($this->numPhotos/$countForPage);
    }

    public function executeShow(sfWebRequest $request) {
        $this->photo = Doctrine_Core::getTable('photo')->find(array($request->getParameter('id')));
        $this->forward404Unless($this->photo);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new photoForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));
        $this->form = new photoForm();
    
        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($photo = Doctrine_Core::getTable('photo')->find(array($request->getParameter('id'))), sprintf('Object photo does not exist (%s).', $request->getParameter('id')));
        $this->form = new photoForm($photo);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($photo = Doctrine_Core::getTable('photo')->find(array($request->getParameter('id'))), sprintf('Object photo does not exist (%s).', $request->getParameter('id')));
        $this->form = new photoForm($photo);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($photo = Doctrine_Core::getTable('photo')->find(array($request->getParameter('id'))), sprintf('Object photo does not exist (%s).', $request->getParameter('id')));
        $photo->delete();

        $this->redirect('photo/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $photo = $form->save();

            $this->redirect('photo/edit?id=' . $photo->getId());
        }
    }

}
